/*
 * Copyright (c) 2022 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
import IllegalArgumentException from '../../IllegalArgumentException.js';
import IllegalStateException from '../../IllegalStateException.js';
import SymbolShapeHint from './SymbolShapeHint.js';
/**
 * Symbol info table for DataMatrix.
 *
 * @version $Id$
 */
/*public*/
export default class SymbolInfo {
    static getPROD_SYMBOLS() {
        return PROD_SYMBOLS;
    }
    /**
     * Overrides the symbol info set used by this class. Used for testing purposes.
     *
     * @param override the symbol info set to use
     */
    static overrideSymbolSet(override) {
        symbols = override;
    }
    static newSymbolInfo(rectangular, dataCapacity, errorCodewords, matrixWidth, matrixHeight, dataRegions) {
        return new SymbolInfo(rectangular, dataCapacity, errorCodewords, matrixWidth, matrixHeight, dataRegions, dataCapacity, errorCodewords);
    }
    constructor(rectangular, dataCapacity, errorCodewords, matrixWidth, matrixHeight, dataRegions, rsBlockData, rsBlockError) {
        this.rectangular = rectangular;
        this.dataCapacity = dataCapacity;
        this.errorCodewords = errorCodewords;
        this.matrixWidth = matrixWidth;
        this.matrixHeight = matrixHeight;
        this.dataRegions = dataRegions;
        this.rsBlockData = rsBlockData;
        this.rsBlockError = rsBlockError;
    }
    static lookupOneArgs(dataCodewords) {
        return SymbolInfo.lookupThreeArgs(dataCodewords, SymbolShapeHint.FORCE_NONE, true);
    }
    static lookupTwoArgs(dataCodewords, shape) {
        return SymbolInfo.lookupThreeArgs(dataCodewords, shape, true);
    }
    static lookupThreeArgsWithBoolean(dataCodewords, allowRectangular, fail) {
        let shape = allowRectangular
            ? SymbolShapeHint.FORCE_NONE : SymbolShapeHint.FORCE_SQUARE;
        return SymbolInfo.lookupThreeArgs(dataCodewords, shape, fail);
    }
    static lookupThreeArgs(dataCodewords, shape, fail) {
        return SymbolInfo.lookup(dataCodewords, shape, null, null, fail);
    }
    static lookup(dataCodewords, shape, minSize, maxSize, fail) {
        for (let symbol /*: SymbolInfo*/ of symbols) {
            if (shape === SymbolShapeHint.FORCE_SQUARE && symbol.rectangular) {
                continue;
            }
            if (shape === SymbolShapeHint.FORCE_RECTANGLE && !symbol.rectangular) {
                continue;
            }
            if (minSize != null
                && (symbol.getSymbolWidth() < minSize.getWidth()
                    || symbol.getSymbolHeight() < minSize.getHeight())) {
                continue;
            }
            if (maxSize != null
                && (symbol.getSymbolWidth() > maxSize.getWidth()
                    || symbol.getSymbolHeight() > maxSize.getHeight())) {
                continue;
            }
            if (dataCodewords <= symbol.dataCapacity) {
                return symbol;
            }
        }
        if (fail) {
            throw new IllegalArgumentException('Can\'t find a symbol arrangement that matches the message. Data codewords: '
                + dataCodewords);
        }
        return null;
    }
    getHorizontalDataRegions() {
        switch (this.dataRegions) {
            case 1:
                return 1;
            case 2:
            case 4:
                return 2;
            case 16:
                return 4;
            case 36:
                return 6;
            default:
                throw new IllegalStateException('Cannot handle this number of data regions');
        }
    }
    getVerticalDataRegions() {
        switch (this.dataRegions) {
            case 1:
            case 2:
                return 1;
            case 4:
                return 2;
            case 16:
                return 4;
            case 36:
                return 6;
            default:
                throw new IllegalStateException('Cannot handle this number of data regions');
        }
    }
    getSymbolDataWidth() {
        return this.getHorizontalDataRegions() * this.matrixWidth;
    }
    getSymbolDataHeight() {
        return this.getVerticalDataRegions() * this.matrixHeight;
    }
    getSymbolWidth() {
        return this.getSymbolDataWidth() + (this.getHorizontalDataRegions() * 2);
    }
    getSymbolHeight() {
        return this.getSymbolDataHeight() + (this.getVerticalDataRegions() * 2);
    }
    getCodewordCount() {
        return this.dataCapacity + this.errorCodewords;
    }
    getInterleavedBlockCount() {
        return this.dataCapacity / this.rsBlockData;
    }
    getDataCapacity() {
        return this.dataCapacity;
    }
    getErrorCodewords() {
        return this.errorCodewords;
    }
    getDataLengthForInterleavedBlock(index) {
        return this.rsBlockData;
    }
    getErrorLengthForInterleavedBlock(index) {
        return this.rsBlockError;
    }
    // @Override
    toString() {
        return (this.rectangular ? 'Rectangular Symbol:' : 'Square Symbol:') +
            ' data region ' + this.matrixWidth + 'x' + this.matrixHeight +
            ', symbol size ' + this.getSymbolWidth() + 'x' + this.getSymbolHeight() +
            ', symbol data size ' + this.getSymbolDataWidth() + 'x' + this.getSymbolDataHeight() +
            ', codewords ' + this.dataCapacity + '+' + this.errorCodewords;
    }
}
class DataMatrixSymbolInfo144 extends SymbolInfo {
    constructor() {
        super(false, 1558, 620, 22, 22, 36, -1, 62);
    }
    // @Override
    getInterleavedBlockCount() {
        return 10;
    }
    // @Override
    getDataLengthForInterleavedBlock(index) {
        return (index <= 8) ? 156 : 155;
    }
}
// @ts-ignore
// @ts-ignore
let PROD_SYMBOLS = Array.from([
    SymbolInfo.newSymbolInfo(false, 3, 5, 8, 8, 1),
    SymbolInfo.newSymbolInfo(false, 5, 7, 10, 10, 1),
    /*rect*/ SymbolInfo.newSymbolInfo(true, 5, 7, 16, 6, 1),
    SymbolInfo.newSymbolInfo(false, 8, 10, 12, 12, 1),
    /*rect*/ SymbolInfo.newSymbolInfo(true, 10, 11, 14, 6, 2),
    SymbolInfo.newSymbolInfo(false, 12, 12, 14, 14, 1),
    /*rect*/ SymbolInfo.newSymbolInfo(true, 16, 14, 24, 10, 1),
    SymbolInfo.newSymbolInfo(false, 18, 14, 16, 16, 1),
    SymbolInfo.newSymbolInfo(false, 22, 18, 18, 18, 1),
    /*rect*/ SymbolInfo.newSymbolInfo(true, 22, 18, 16, 10, 2),
    SymbolInfo.newSymbolInfo(false, 30, 20, 20, 20, 1),
    /*rect*/ SymbolInfo.newSymbolInfo(true, 32, 24, 16, 14, 2),
    SymbolInfo.newSymbolInfo(false, 36, 24, 22, 22, 1),
    SymbolInfo.newSymbolInfo(false, 44, 28, 24, 24, 1),
    /*rect*/ SymbolInfo.newSymbolInfo(true, 49, 28, 22, 14, 2),
    SymbolInfo.newSymbolInfo(false, 62, 36, 14, 14, 4),
    SymbolInfo.newSymbolInfo(false, 86, 42, 16, 16, 4),
    SymbolInfo.newSymbolInfo(false, 114, 48, 18, 18, 4),
    SymbolInfo.newSymbolInfo(false, 144, 56, 20, 20, 4),
    SymbolInfo.newSymbolInfo(false, 174, 68, 22, 22, 4),
    new SymbolInfo(false, 204, 84, 24, 24, 4, 102, 42),
    new SymbolInfo(false, 280, 112, 14, 14, 16, 140, 56),
    new SymbolInfo(false, 368, 144, 16, 16, 16, 92, 36),
    new SymbolInfo(false, 456, 192, 18, 18, 16, 114, 48),
    new SymbolInfo(false, 576, 224, 20, 20, 16, 144, 56),
    new SymbolInfo(false, 696, 272, 22, 22, 16, 174, 68),
    new SymbolInfo(false, 816, 336, 24, 24, 16, 136, 56),
    new SymbolInfo(false, 1050, 408, 18, 18, 36, 175, 68),
    new SymbolInfo(false, 1304, 496, 20, 20, 36, 163, 62),
    new DataMatrixSymbolInfo144()
]);
let symbols = PROD_SYMBOLS;
